<template>
    <el-row >
      <el-col :span="4">
        <el-image :src="require('@/assets/img.png')" class="captchaImg"></el-image>
      </el-col>
      <el-col :span="8">
        <el-form  :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item    style="width: 400px">
            <h2>Vue后台管理系统</h2>
          </el-form-item>
          <el-form-item label="用户名" prop="username"  style="width: 400px">
            <el-input type="text" v-model="ruleForm.username" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" style="width: 400px">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="code" style="width: 400px">
            <el-input v-model.number="ruleForm.code" style="width: 150px;float: left" clearable></el-input>
            <el-image :src="captchaImg" class="captchaImg"></el-image>
          </el-form-item>
          <el-form-item style="width: 400px">
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="12"></el-col>
    </el-row>
</template>

<script>

import qs from "qs";

export default {
  name: "Login",
  data() {

    return {
      captchaImg: null,
      ruleForm: {
        username: '',
        password: '',
        code: '',
        tokens: ''
      },
      rules: {
        username: [
          {  required: true, message: '请输入用户名', trigger: 'blur'  },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          {  required: true, message: '请输入密码', trigger: 'blur'  },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
        code: [
          {  required: true, message: '请输入验证码', trigger: 'blur'  },
          { min: 5, max: 5, message: '长度为5个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      //先校验表单项,再执行后续操作
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/login?'+qs.stringify(this.ruleForm)).then(res=>{
            //从header中获取后端token
            const jwt = res.headers['authorization']
            //将token这种公共变量存放在store中
            this.$store.commit("SET_TOKEN",jwt);

            console.log('token')
            console.log(this.$store.state.token)
            //跳转到首页
            this.$router.push('/index');
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //获取验证码图片
    getCaptcha(){
      this.$axios.get('/captcha').then(res=>{
        console.log(res)
        this.ruleForm.tokens = res.data.data.token;
        this.captchaImg = res.data.data.captchaImg;
      })
    },
  },
  created() {
    this.getCaptcha();
  }
}
</script>

<style scoped>
.el-row{
  background-image: url("../assets/bcg.jpg");
  /*整张图片显示*/
  background-size: cover;
  height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
}

/*普通图片及验证码图片格式*/
.captchaImg{
  float: left;
  margin-left: 10px;
  /*  四角圆角轮廓*/
  border-radius: 4px;
}
</style>